import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @description: TODO
* @author hewei
* @date 2022/9/20 15:17
* @version 1.0
*/

public class WordSubsets {

    public List<String> wordSubsets(String[] words1, String[] words2) {
        int[] temp = new int[26];
        int[] count = new int[26];
        for (String s : words2) {
            Arrays.fill(temp, 0);
            for (int i = 0; i < s.length(); i++) {
                int index = s.charAt(i) - 'a';
                temp[index]++;
                count[index] = Math.max(temp[index], count[index]);
            }
        }
        List<String> ans = new ArrayList<>();
        for (String s : words1) {
            Arrays.fill(temp, 0);
            for (int i = 0; i < s.length(); i++) {
                int index = s.charAt(i) - 'a';
                temp[index]++;
            }
            for (int i = 0; i < 26; i++) {
                if (count[i] > temp[i]) break;
                if (i == 25) ans.add(s);
            }
        }
        return ans;
    }
}
